# UI自动化管理工具 Makefile

.PHONY: help install install-dev run clean test lint format check docs build upload

# 默认目标
help:
	@echo "UI自动化管理工具 - 可用命令:"
	@echo ""
	@echo "安装和运行:"
	@echo "  install      - 安装项目依赖"
	@echo "  install-dev  - 安装开发依赖"
	@echo "  run          - 运行应用程序"
	@echo ""
	@echo "开发工具:"
	@echo "  test         - 运行测试"
	@echo "  lint         - 运行代码检查"
	@echo "  format       - 格式化代码"
	@echo "  check        - 运行所有检查"
	@echo ""
	@echo "文档和构建:"
	@echo "  docs         - 生成文档"
	@echo "  build        - 构建分发包"
	@echo "  upload       - 上传到PyPI"
	@echo ""
	@echo "维护:"
	@echo "  clean        - 清理临时文件"
	@echo "  setup        - 初始化开发环境"

# 安装项目依赖
install:
	pip install -r requirements.txt
	playwright install

# 安装开发依赖
install-dev:
	pip install -e ".[dev,docs]"
	playwright install

# 运行应用程序
run:
	python main.py

# 初始化开发环境
setup: install-dev
	@echo "正在初始化开发环境..."
	@mkdir -p logs
	@mkdir -p scripts/active
	@mkdir -p scripts/archived
	@mkdir -p scripts/drafts
	@cp .env.example .env
	@echo "开发环境初始化完成！"
	@echo "请编辑 .env 文件配置您的环境变量"

# 运行测试
test:
	pytest tests/ -v --cov=backend --cov=frontend --cov=config --cov=utils

# 运行代码检查
lint:
	flake8 backend frontend config utils main.py
	mypy backend frontend config utils main.py

# 格式化代码
format:
	black backend frontend config utils main.py
	isort backend frontend config utils main.py

# 运行所有检查
check: lint test
	@echo "所有检查通过！"

# 生成文档
docs:
	@echo "生成文档..."
	sphinx-build -b html docs docs/_build/html

# 构建分发包
build: clean
	python -m build

# 上传到PyPI
upload: build
	python -m twine upload dist/*

# 清理临时文件
clean:
	@echo "清理临时文件..."
	find . -type f -name "*.pyc" -delete
	find . -type d -name "__pycache__" -delete
	find . -type d -name "*.egg-info" -exec rm -rf {} +
	rm -rf build/
	rm -rf dist/
	rm -rf .coverage
	rm -rf .pytest_cache/
	rm -rf .mypy_cache/
	rm -rf docs/_build/

# 开发模式运行（带调试）
dev:
	DEBUG=true python main.py

# 安装Git钩子
install-hooks:
	@echo "安装Git预提交钩子..."
	@echo '#!/bin/sh' > .git/hooks/pre-commit
	@echo 'make check' >> .git/hooks/pre-commit
	@chmod +x .git/hooks/pre-commit
	@echo "Git钩子安装完成！"

# 更新依赖
update-deps:
	pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
	pip freeze > requirements.txt

# 启动Playwright的代码生成器
playwright-codegen:
	playwright codegen

# 安装浏览器
install-browsers:
	playwright install

# 生成项目统计
stats:
	@echo "项目统计信息:"
	@echo "=============="
	@echo -n "Python 文件: "
	@find . -name "*.py" | wc -l
	@echo -n "代码行数: "
	@find . -name "*.py" -exec cat {} \; | wc -l
	@echo -n "测试文件: "
	@find . -name "test_*.py" -o -name "*_test.py" | wc -l
	@echo -n "脚本数量: "
	@find scripts -name "*.json" 2>/dev/null | wc -l || echo "0"